package com.uls.web.controller.czj;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.czj.domain.CzjRoom;
import com.uls.czj.service.ICzjRoomService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.czj.domain.CzjFloor;
import com.uls.czj.service.ICzjFloorService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 楼层管理Controller
 * 
 * @author czj
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/czj/floor")
public class CzjFloorController extends BaseController
{
    @Autowired
    private ICzjFloorService czjFloorService;

    @Autowired
    private ICzjRoomService czjRoomService;

    /**
     * 查询楼层管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:floor:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzjFloor czjFloor)
    {
        startPage();
        List<CzjFloor> list = czjFloorService.selectCzjFloorList(czjFloor);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('czj:floor:list')")
    @GetMapping("/getbuildingid/{getbuildingid}")
    public TableDataInfo getbuildingid(@PathVariable("getbuildingid") int getbuildingid)
    {
        CzjFloor czjFloor = new CzjFloor();
        czjFloor.setBuildingid((long) getbuildingid);
        startPage();
        List<CzjFloor> list = czjFloorService.selectCzjFloorList(czjFloor);

        CzjRoom czjRoom = new CzjRoom();
        List<CzjFloor> list1 = new ArrayList<>();

        for (CzjFloor czjFloor1:list){
            czjRoom.setFloorid(czjFloor1.getFloorid());
            czjRoom.setStatus("0");
            czjFloor1.setStatusVacant((long) czjRoomService.selectCzjRoomList(czjRoom).size());
            czjRoom.setStatus("1");
            czjFloor1.setStatusUse((long) czjRoomService.selectCzjRoomList(czjRoom).size());
            czjRoom.setStatus("2");
            czjFloor1.setStatusRepair((long) czjRoomService.selectCzjRoomList(czjRoom).size());
            czjFloorService.updateCzjFloor(czjFloor1);
            list1.add(czjFloor1);
        }
        if (list1.size()==0){
            CzjFloor czjFloor1 = new CzjFloor();
            czjFloor1.setBuildingid((long) getbuildingid);
            list1.add(czjFloor1);
        }
        return getDataTable(list1);
    }

    /**
     * 导出楼层管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:floor:export')")
    @Log(title = "楼层管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzjFloor czjFloor)
    {
        List<CzjFloor> list = czjFloorService.selectCzjFloorList(czjFloor);
        ExcelUtil<CzjFloor> util = new ExcelUtil<CzjFloor>(CzjFloor.class);
        util.exportExcel(response, list, "楼层管理数据");
    }

    /**
     * 获取楼层管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('czj:floor:query')")
    @GetMapping(value = "/{floorid}")
    public AjaxResult getInfo(@PathVariable("floorid") Long floorid)
    {
        return success(czjFloorService.selectCzjFloorByFloorid(floorid));
    }

    /**
     * 新增楼层管理
     */
    @PreAuthorize("@ss.hasPermi('czj:floor:add')")
    @Log(title = "楼层管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzjFloor czjFloor)
    {

        czjFloor.setFloorid(czjFloor.getBuildingid()*100+czjFloor.getFloornumber());
        return toAjax(czjFloorService.insertCzjFloor(czjFloor));
    }

    /**
     * 修改楼层管理
     */
    @PreAuthorize("@ss.hasPermi('czj:floor:edit')")
    @Log(title = "楼层管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzjFloor czjFloor)
    {
        return toAjax(czjFloorService.updateCzjFloor(czjFloor));
    }

    /**
     * 删除楼层管理
     */
    @PreAuthorize("@ss.hasPermi('czj:floor:remove')")
    @Log(title = "楼层管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{floorids}")
    public AjaxResult remove(@PathVariable Long[] floorids)
    {
        return toAjax(czjFloorService.deleteCzjFloorByFloorids(floorids));
    }
}
